package stark.app.function.jigsaw;

import android.content.Intent;
import android.net.Uri;

import com.bumptech.glide.Glide;
import com.stark.jigsaw.JigsawEntry;
import com.stark.jigsaw.pinct.BasePinChangTuActivity;
import com.stark.jigsaw.puzzle.BasePuzzleActivity;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import stark.app.R;
import stark.app.databinding.ActivityJigsawDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.constant.Extra;
import stark.common.basic.constant.ThemeMode;

/**
 * Author：daizhongbin on 2021/4/6
 * Email：1027154338@qq.com
 * Desc:
 */
public class JigsawDemoActivity extends BaseNoModelActivity<ActivityJigsawDemoBinding> {

    private static final int REQ_PUZZLE = 1;
    private static final int REQ_PCT = 2;

    @Override
    protected int onCreate() {
        return R.layout.activity_jigsaw_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnPuzzleNight.setOnClickListener(v -> {
            start(BasePuzzleActivity.MIN_IMG_COUNT, BasePuzzleActivity.MAX_IMG_COUNT, paths -> {
                JigsawEntry.enterPuzzle(this, paths);
            });
        });
        mDataBinding.btnPuzzleLight.setOnClickListener(v -> {
            start(BasePuzzleActivity.MIN_IMG_COUNT, BasePuzzleActivity.MAX_IMG_COUNT, paths -> {
                JigsawEntry.enterPuzzle(this, ThemeMode.LIGHT, paths);
            });
        });
        mDataBinding.btnPuzzleRet.setOnClickListener(v -> {
            start(BasePuzzleActivity.MIN_IMG_COUNT, BasePuzzleActivity.MAX_IMG_COUNT, paths -> {
                JigsawEntry.enterPuzzleForRet(this, paths, REQ_PUZZLE);
            });
        });
        mDataBinding.btnPuzzleCallback.setOnClickListener(v -> {
            start(BasePuzzleActivity.MIN_IMG_COUNT, BasePuzzleActivity.MAX_IMG_COUNT, paths -> {
                JigsawEntry.enterPuzzleWithCallback(this, paths, bitmap -> {
                    if (bitmap != null) {
                        mDataBinding.ivRet.setImageBitmap(bitmap);
                    }
                });
            });
        });
        mDataBinding.btnPuzzleCustom.setOnClickListener(v -> {
            start(BasePuzzleActivity.MIN_IMG_COUNT, BasePuzzleActivity.MAX_IMG_COUNT, paths -> {
                SubPuzzleActivity.start(this, SubPuzzleActivity.class, paths);
            });
        });
        mDataBinding.btnPinctNight.setOnClickListener(v -> {
            start(BasePinChangTuActivity.MIN_IMG_COUNT, BasePinChangTuActivity.MAX_IMG_COUNT, paths -> {
                JigsawEntry.enterPinChangTu(this, paths);
            });
        });
        mDataBinding.btnPinctLight.setOnClickListener(v -> {
            start(BasePinChangTuActivity.MIN_IMG_COUNT, BasePinChangTuActivity.MAX_IMG_COUNT, paths -> {
                JigsawEntry.enterPinChangTu(this, ThemeMode.LIGHT, paths, false);
            });
        });
        mDataBinding.btnPinctRet.setOnClickListener(v -> {
            start(BasePinChangTuActivity.MIN_IMG_COUNT, BasePinChangTuActivity.MAX_IMG_COUNT, paths -> {
                JigsawEntry.enterPinChangTuForRet(this, paths, false, REQ_PCT);
            });
        });
        mDataBinding.btnPinctCallback.setOnClickListener(v -> {
            start(BasePinChangTuActivity.MIN_IMG_COUNT, BasePinChangTuActivity.MAX_IMG_COUNT, paths -> {
                JigsawEntry.enterPinChangTuWithCallback(this, paths, false, bitmap -> {
                    if (bitmap != null) {
                        mDataBinding.ivRet.setImageBitmap(bitmap);
                    }
                });
            });
        });
        mDataBinding.btnPinctCustom.setOnClickListener(v -> {
            start(BasePinChangTuActivity.MIN_IMG_COUNT, BasePinChangTuActivity.MAX_IMG_COUNT, paths -> {
                SubPinChangTuActivity.start(this, SubPinChangTuActivity.class, paths, true);
            });
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQ_PUZZLE ||
                    requestCode == REQ_PCT) {
                Uri uri = data.getParcelableExtra(Extra.RET_URI);
                Glide.with(this).load(uri).into(mDataBinding.ivRet);
            }
        }
    }

    private void start(int minCount, int maxCount, ICallback callback) {
        PhotoSelectorBuilder.builder(this)
                .mode(PhotoConfig.Mode.PHOTO)
                .minCount(minCount)
                .maxCount(maxCount)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        ArrayList<String> pathList = new ArrayList<>();
                        for (SelectMediaEntity entity : data) {
                            pathList.add(entity.getTargetPath());
                        }
                        if (callback != null) {
                            callback.onGetPhoto(pathList);
                        }
                    }
                });
    }

    private interface ICallback {

        void onGetPhoto(ArrayList<String> paths);

    }
}
